<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active:activeIndex===index}"
        @click="changeRole(index,item.roleId)"
      >
        <div class="role-info">
          <!-- 没有声明但是可以直接使用，推理大概是一个全局组件 -->
          <svg-icon :icon-class="activeIndex===index?'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <svg-icon icon-class="more" />
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <!-- 右侧权限和成员 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <!-- 权限页面 -->
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <!--
            树形组件使用技巧：
            :props="{label:'title'}"label用于展示树形节点的字段，如果数据中不叫label，可以自定义修改{label:'接口中的字段'}
            show-checkbox展示勾选框
            default-expand-all默认展开全部
            node-key设置节点的唯一标识(类似v-for加key是一个原理)，将数据中的哪个字段用于唯一标识，id是字段不是变量，不用带:
           -->
              <el-tree
                ref="tree"
                :data="item.children"
                show-checkbox
                default-expand-all
                check-strictly
                :props="{label:'title'}"
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${members})`" name="member">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI } from '@/api/system'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      treeList: [],
      roleUserList: [],
      // 当前激活的索引
      activeIndex: 0,
      // 当前激活的id
      activeId: '',
      // 所有树的权限点
      perms: [],
      activeName: 'tree',
      // 所有角色的成员数量
      members: 0
    }
  },
  created() {
    this.getRoleList()
    this.getTreeList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data

      // 3.默认获取第1个角色的权限点
      this.getRoleDetail(this.roleList[0].roleId)
      // 默认获取第一个角色的成员列表
      this.getRoleUser(this.roleList[0].roleId)
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      // 动态生成的属性不具备响应式特性
      const data = [...res.data]
      // 给data动态添加，统一修改treeList
      this.addDisabled(data)
      this.treeList = data
    },
    // 递归添加禁用属性
    addDisabled(data) {
      data.forEach(item => {
        item.disabled = true
        // children必须有东西
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    // 1.点击切换id
    changeRole(index, id) {
      this.activeIndex = index
      this.activeId = id
      this.getRoleDetail(id)
      this.getRoleUser(id)
    },
    // 2.获取角色拥有的id数据
    async getRoleDetail(id) {
      const res = await getRoleDetailAPI(id)
      this.perms = res.data.perms
      // 给每一个树回填权限点
      this.$refs.tree.forEach((tree, index) => {
        tree.setCheckedKeys(this.perms[index])
      })
    },
    // 获取角色对应的人员
    async getRoleUser(id) {
      const res = await getRoleUserAPI(id)
      console.log(res)
      this.members = res.data.total
      this.roleUserList = res.data.rows
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
